package com.googlecode.tracey.ui;

import com.googlecode.tracey.ui.core.Extensions;
import com.googlecode.tracey.ui.core.ExtensionsModule;
import com.googlecode.tracey.ui.core.MessageServerModule;
import com.googlecode.tracey.ui.core.MessageServers;
import com.googlecode.utterlyidle.Application;
import com.googlecode.utterlyidle.Resources;
import com.googlecode.utterlyidle.Response;
import com.googlecode.utterlyidle.annotations.*;
import com.googlecode.utterlyidle.httpserver.RestServer;
import com.googlecode.utterlyidle.modules.Module;
import com.googlecode.utterlyidle.modules.ResourcesModule;

import static com.googlecode.funclate.Model.mutable.model;
import static com.googlecode.totallylazy.Bytes.bytes;
import static com.googlecode.tracey.ui.core.Extension.extension;
import static com.googlecode.tracey.ui.core.MessageServer.messageServer;
import static com.googlecode.utterlyidle.BasePath.basePath;
import static com.googlecode.utterlyidle.HttpHeaders.CONTENT_TYPE;
import static com.googlecode.utterlyidle.MediaType.APPLICATION_JSON;
import static com.googlecode.utterlyidle.MediaType.TEXT_HTML;
import static com.googlecode.utterlyidle.ResponseBuilder.response;
import static com.googlecode.utterlyidle.ServerConfiguration.defaultConfiguration;
import static com.googlecode.utterlyidle.Status.OK;
import static com.googlecode.utterlyidle.annotations.AnnotatedBindings.annotatedClass;
import static java.util.UUID.randomUUID;

public class ShowAndTell {

    public static void main(String[] args) throws Exception {
        Application application = new WebApplication(basePath("/"), new ShowAndTellModule());
        new RestServer(application, defaultConfiguration().port(9000));
    }

    private static class ShowAndTellModule implements MessageServerModule, ExtensionsModule, ResourcesModule {
        public MessageServers addMessageServers(MessageServers messageServers) throws Exception {
            return messageServers
                    .add(messageServer("Show and Tell", "http://localhost:9000/show-and-tell"))
                    .add(messageServer("Production", "http://badger-tracey.sns.sky.com:8000/messages"))
                    ;
        }

        public Module addResources(Resources resources) throws Exception {
            resources.add(annotatedClass(ShowAndTellMessagesResource.class));
            return this;
        }

        @Override
        public Extensions addExtension(Extensions extensions) throws Exception {
            return extensions.add(extension("make-printable", "/static/make-printable.html"));
        }


        @Path("show-and-tell")
        @Produces(APPLICATION_JSON)
        public static class ShowAndTellMessagesResource {

            @GET
            @Produces(TEXT_HTML)
            public Response searchForm() {
                return response().status(OK).header(CONTENT_TYPE, TEXT_HTML).entity(bytes(getClass().getResourceAsStream("showAndTellSearchForm.html"))).build();
            }

            @POST
            @Path("search")
            public String search(@FormParam("orderId") String q) {
                return model()
                        .add("messages", model()
                                .add("from", "client")
                                .add("to", "server")
                                .add("name", "Booking Request")
                                .add("body", "<request><stuff></stuff></request>")
                                .add("messageId", randomUUID().toString()))
                        .add("messages",
                                model()
                                        .add("from", "server")
                                        .add("to", "client")
                                        .add("name", "Booking")
                                        .add("body", "<booking><id>" + q + "</id></booking>")
                                        .add("messageId", randomUUID().toString())).toString();
            }

            @POST
            @Path("search")
            public String byTraceyIds(@FormParam("traceyId") String traceyId) {
                return model()
                        .add("messages", model()
                                .add("from", "MiddleOffice")
                                .add("to", "BackOffice")
                                .add("name", "Reconcilation Request")
                                .add("body", "<request><stuff></stuff></request>")
                                .add("messageId", randomUUID().toString()))
                        .add("messages",
                                model()
                                        .add("from", "BackOffice")
                                        .add("to", "MiddleOffice")
                                        .add("name", "Acknowledgement")
                                        .add("body", "<ack><id>12312321</id></ack>")
                                        .add("messageId", randomUUID().toString()))
                        .add("messages",
                                model()
                                        .add("from", "BackOffice")
                                        .add("to", "MiddleOffice")
                                        .add("name", "Complete")
                                        .add("body", "<complete><id>12312321</id></complete>")
                                        .add("messageId", randomUUID().toString()))
                        .toString();
            }
        }
    }
}